package com.bigtotoro.shuiyincamera.video.trim;

import android.app.ProgressDialog;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bigtotoro.shuiyincamera.BaseActivity;
import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.util.MyUtils;
import com.bigtotoro.shuiyincamera.video.VideoAddWater2Activity;
import com.bigtotoro.shuiyincamera.video.trim.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import qiu.niorgai.StatusBarCompat;

/**
 * Author：J.Chou
 * Date：  2016.08.01 2:23 PM
 * Email： who_know_me@163.com
 * Describe:
 */
public class VideoTrimmerActivity extends BaseActivity implements VideoTrimListener {

    private static final String TAG = "video-clip";
    public static final String VIDEO_PATH_KEY = "video-file-path";
    private static final String COMPRESSED_VIDEO_FILE_NAME = "compress.mp4";
    public static final int VIDEO_TRIM_REQUEST_CODE = 0x001;
    private VideoTrimmerView trimmerView;
    private ProgressDialog mProgressDialog;

    public String mode;
    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_trim);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.font_0));
        tv_title = findViewById(R.id.tv_title);
        if (getIntent().getStringExtra("mode")!=null){
            mode = getIntent().getStringExtra("mode");  //splice_edit,extract_bg,daofang,gif
            if (mode!=null && mode.equals("extract_bg")){
                tv_title.setText("分离音频");
                //添加友盟事件统计
                MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_extract_bg","分离音频");
            }else if (mode!=null && mode.equals("splice_edit")){ //视频拼接界面编辑单个视频
                tv_title.setText("视频分割");

            }else if (mode!=null && mode.equals("daofang")){ //视频拼接界面编辑单个视频
                tv_title.setText("视频倒放");
                //添加友盟事件统计
                MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_daofang","视频倒放");
            }else if (mode!=null && mode.equals("gif")){ //视频拼接界面编辑单个视频
                tv_title.setText("GIF导出");
                //添加友盟事件统计
                MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_gif","视频GIF导出");
            }else {
                tv_title.setText("视频分割");
                //添加友盟事件统计
                MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_Trim","视频分割");
            }
        }

        initBase();
        initView();//onSaveClicked
    }

    public String getMode(){
        return mode;
    }

    public String videoPath;
    public void initView() {
        trimmerView = findViewById(R.id.trimmer_view);
        Bundle bd = getIntent().getExtras();
        String path = "";
        if (bd != null){
            path = bd.getString(VIDEO_PATH_KEY);
            videoPath = path;
        }
        if (trimmerView != null) {
            trimmerView.initVideoPath(videoPath);
            trimmerView.setOnTrimVideoListener(this);
            trimmerView.initVideoByURI(Uri.parse(path));
            trimmerView.initActivity(VideoTrimmerActivity.this);
        }

        findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                trimmerView.onSaveClicked(mode); //保存剪辑后的视频 //splice_edit,extract_bg
                if (mode!=null && mode.equals("extract_bg")){
                    //添加友盟事件统计
                    MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_extract_bg_save","分离音频保存");
                }else if (mode!=null && mode.equals("splice_edit")){ //视频拼接界面编辑单个视频

                }else if (mode!=null && mode.equals("daofang")){ //视频拼接界面编辑单个视频

                    //添加友盟事件统计
                    MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_daofang_save","视频倒放保存");
                }else if (mode!=null && mode.equals("gif")){ //视频拼接界面编辑单个视频
                    //添加友盟事件统计
                    MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_gif_save","视频GIF导出保存");

                }else {
                    //添加友盟事件统计
                    MyUtils.addUmengEvent(VideoTrimmerActivity.this,"video_Trim_save","视频分割保存");
                }
            }
        });
    }




    @Override
    public void onStartTrim() {
        buildDialog(getResources().getString(R.string.trimming)).show();
    }

    @Override
    public void onFinishTrim(String in) {
        if (mProgressDialog.isShowing()) mProgressDialog.dismiss();
        ToastUtil.longShow(this, getString(R.string.trimmed_done));
        finish();
        //TODO: please handle your trimmed video url here!!!
    }

    @Override
    public void onCancel() {
        trimmerView.onDestroy();
        finish();
    }



    private ProgressDialog buildDialog(String msg) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(this, "", msg);
        }
        mProgressDialog.setMessage(msg);
        return mProgressDialog;
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("VideoTrimmerActivity");//统计页面
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("VideoTrimmerActivity");//统计页面
        MobclickAgent.onPause(this);
        trimmerView.onVideoPause();
        trimmerView.setRestoreState(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        trimmerView.onDestroy();
    }


}
